﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace JYH.Business
{
    /// <summary>
    /// 系统错误码枚举
    /// </summary>
    public enum ErrorCode
    {
        /// <summary>
        /// {0}  自定义
        /// </summary>
        [Description("{0}")]
        Customize = -1,

        /// <summary>
        /// 条码{0}已经在库
        /// </summary>
        [Description("条码{0}已经在库")]
        StockExistBarcode = 0,

        /// <summary>
        /// 密码错误
        /// </summary>
        [Description("密码错误")]
        Login002 = 1,

        /// <summary>
        /// 条码{0}不存在
        /// </summary>
        [Description("条码{0}不存在")]
        StockNoExistBarcode = 2,

        /// <summary>
        /// {0}物料条码不存在
        /// </summary>
        [Description("{0}物料条码不存在")]
        NoExistBarcode = 3,

        /// <summary>
        /// {0}仓库必须有一个及以上的储位
        /// </summary>
        [Description("{0}仓库必须有一个及以上的储位")]
        WarehouseInfo03 = 4,

        /// <summary>
        /// {0}已经占用
        /// </summary>
        [Description("{0}已经占用")]
        LamplInfo01 = 5,

        /// <summary>
        /// {0}用户不存在
        /// </summary>
        [Description("{0}用户不存在")]
        Login001 = 6,

        /// <summary>
        /// {0}储位不存在
        /// </summary>
        [Description("{0}储位不存在")]
        NoExistStorage = 7,

        /// <summary>
        /// {0}储位不在{1}仓库中
        /// </summary>
        [Description("{0}储位不在{1}仓库中")]
        WarehouseInfo02 = 8,

        /// <summary>
        /// {0}不存在
        /// </summary>
        [Description("{0}不存在")]
        WarehouseInfo01 = 9,

        /// <summary>
        /// {0}账户状态异常
        /// </summary>
        [Description("{0}账户状态异常")]
        Login003 = 10,

        /// <summary>
        /// {0}灯珠不存在
        /// </summary>
        [Description("{0}灯珠不存在")]
        NoExistLamp = 11,

        /// <summary>
        /// {0}已被删除
        /// </summary>
        [Description("{0}已被删除")]
        Delete = 12,

        /// <summary>
        /// {0}格式不正确
        /// </summary>
        [Description("{0}格式不正确")]
        Format = 13,

        /// <summary>
        /// 请联系管理员
        /// </summary>
        [Description("Please contact the system service provider.")]
        Admin = 14
    }

    /// <summary>
    /// 错误码扩展方法
    /// </summary>
    public static class ErrorCodeExtensions
    {
        /// <summary>
        /// 获取错误码对应的提示信息
        /// </summary>
        /// <param name="errorCode">错误码</param>
        /// <param name="args">格式化参数</param>
        /// <returns>格式化后的提示信息</returns>
        public static string GetMessage(this ErrorCode errorCode, params object[] args)
        {
            var field = errorCode.GetType().GetField(errorCode.ToString());
            var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
            return attribute == null ? errorCode.ToString() : string.Format(attribute.Description, args);
        }
    }
}